<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author hancing.com
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="false"
    :okAuth="'dp:kccode:dpKechengcode:edit'"
    @register="registerDrawer"
    
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
    <div class="flex justify-end mr-4 mb-2">
       <a-button type="primary" @click="handleSubmit" >提交保存</a-button>
    </div>
    <BasicTable @register="registerTable">
      
      
    </BasicTable>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsDpKccodeDpKechengcodeForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { DpKechengcode, dpKechengcodeSave, dpKechengcodeForm } from '/@/api/dp/kccode/dpKechengcode';
    import { dpKechengcodeDelete, dpKechengcodeListData } from '/@/api/dp/kccode/dpKechengcode';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('dp.kccode.dpKechengcode');
  const { showMessage } = useMessage();
  const record = ref<DpKechengcode>({} as DpKechengcode);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('课程观看码') : t('课程观看码'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('课程ID'),
      field: 'kcid',
      show:false,
      component: 'Input',
      componentProps: {
        maxlength: 36,
      },
      required: true,
    },
    {
      label: t('观看码'),
      field: 'kccode',
      component: 'InputTextArea',
      componentProps: {
       
        placeholder: '请输入课程激活观看码,回车换行可添加多条',
        maxlength: 500,
      },
      colProps: { lg: 24, md: 24 },
      required: true,
    },
    
  ];
  const tableColumns: BasicColumn[] = [
    
    {
      title: t('观看码'),
      dataIndex: 'kccode',
      key: 'a.kccode',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('码状态'),
      dataIndex: 'codestatus',
      key: 'a.codestatus',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'dp_kccode_status',
    },
    {
      title: t('购买/锁定用户'),
      dataIndex: 'uno',
      key: 'a.uno',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 60,
    actions: (record: Recordable) => [
    
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除观看码'),
        popConfirm: {
          title: t('是否确认删除观看码'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'dp:kccode:dpKechengcode:edit',
      },
    ],
  };
  
  async function handleDelete(record: Recordable) {
    const res = await dpKechengcodeDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
  const [registerTable, { reload, getForm }] = useTable({
    api: dpKechengcodeListData,
    beforeFetch: (params) => {
      
      params.kcid=nowkcid.value;
      return params;
    },
    columns: tableColumns,
    actionColumn:actionColumn,
    formConfig: [],
    showTableSetting: false,
    useSearchForm: false,
    canResize: false,
  });
  
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });
  const nowkcid=ref("");
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
   
    nowkcid.value=data.kcid;
    
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await dpKechengcodeForm(data);
    record.value = (res.dpKechengcode || {}) as DpKechengcode;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
    reload();
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await dpKechengcodeSave(params, data);
      showMessage(res.message);
      //setTimeout(closeDrawer);
      //emit('success', data);

      //刷新
      reload();
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
